package cn.well.cloud.generator.action;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.util.StrUtil;
import cn.well.cloud.generator.action.model.GenQo;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

/**
 * 默认的代码生成的配置
 *
 * @author fengshuonan
 * @date 2017-10-28-下午8:27
 */
public class GunsGeneratorConfig extends AbstractGeneratorConfig {
    private GenQo genQo;

    public GunsGeneratorConfig(GenQo genQo) {
        genQo.setProjectPackage(contextConfig.getProPackage()+"."+contextConfig.getModuleName());
        this.genQo = genQo;
    }
    
    @Override
    protected void config() {
        super.initConfig();
        
        dataSourceConfig();
        strategyConfig();
        globalConfig();
        packageConfig();
        contextConfig();
        sqlConfig();
    }
    
   // mybatis-plus 数据源配置
    protected void dataSourceConfig() {
        dataSourceConfig.setDbType(DbType.MYSQL)
        .setDriverName("com.mysql.cj.jdbc.Driver")
        .setUrl("jdbc:mysql://localhost:3306/well_cloud_cdkey?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=CONVERT_TO_NULL&useSSL=false&serverTimezone=CTT")
        .setUsername("root")
        .setPassword("123456");
        dataSourceConfig.setDriverName("com.mysql.jdbc.Driver");	
    }
   // mybatis-plus 策略配置,更多配置代码生成时会重写
    protected void strategyConfig() {
        /**
         * 生成策略
         */
        // 自定义需要填充的字段
        List<TableFill> tableFillList = new ArrayList<>();
        
        tableFillList.add(new TableFill("del_flag", FieldFill.INSERT));
        tableFillList.add(new TableFill("create_user", FieldFill.INSERT));
        tableFillList.add(new TableFill("create_time", FieldFill.INSERT));
        tableFillList.add(new TableFill("update_user", FieldFill.INSERT_UPDATE));
        tableFillList.add(new TableFill("update_time", FieldFill.INSERT_UPDATE));
        
        // 此处可以修改为您的表前缀
        if (genQo.getPrefix() != null) {
            strategyConfig.setTablePrefix(genQo.getPrefix());
        }
        strategyConfig.setTablePrefix(genQo.getPrefix())
        //lombok
        .setEntityLombokModel(true)   
        //表名生成策略
        .setNaming(NamingStrategy.underline_to_camel)
        //需要生成的的表名，多个表名传数组
        .setInclude(genQo.getTableName())
        .setTableFillList(tableFillList)
        .setEntityTableFieldAnnotationEnable(true)
        .setEntityBooleanColumnRemoveIsPrefix(true)
        ;
    }
    
    // mybatis-plus 全局配置
    protected void globalConfig() {
        globalConfig
        .setBaseColumnList(true)// XML columList
        .setBaseResultMap(true)//XML ResultMap
        .setEnableCache(false) // XML 二级缓存
        .setActiveRecord(true) //ActiveRecord开启,实体会继承Model<T>
        .setFileOverride(true)
        .setOpen(false)//不打开输出目录
        .setOutputDir(StrUtil.isEmpty(genQo.getProjectPath())?System.getProperty("user.dir"):genQo.getProjectPath()+"/src/main/java/")
        .setAuthor(genQo.getAuthor())
        .setIdType(IdType.ASSIGN_ID)
        .setDateType(DateType.ONLY_DATE)
        .setSwagger2(true)
        ;
    } 
    // mybatis-plus 自定义模板配置,用来控制plus代码是否覆盖
    protected void templateConfig() {
    	templateConfig.setController("java.com.SpringBoot.ibats.controller");
    	templateConfig.setEntity("java.com.SpringBoot.ibats.model.Entity");
    	templateConfig.setMapper("java.com.SpringBoot.ibats.mapper");
    	templateConfig.setXml("src.resources.sqlBaties");
    	templateConfig.setService("java.com.SpringBoot.ibats.service");
    	templateConfig.setServiceImpl("java.com.SpringBoot.ibats.service.impl");
    }
    
    // mybatis-plus 包配置
    protected void packageConfig() {
        packageConfig.setParent(genQo.getProjectPackage()) 
        .setEntity("model")
        .setMapper("mapper")
        .setXml("mapper.mapping")
        .setService("service")
        .setServiceImpl("service.impl")
        ;
    }

    //自定义配置
    protected void contextConfig() {
        contextConfig.setServiceName(genQo.getServiceName());
        contextConfig.setAuthor(genQo.getAuthor());// 作者,在使用模板生成是套用
        contextConfig.setBizChName(genQo.getBizName());//业务名
        contextConfig.setBizEnName(StrUtil.toCamelCase(StrUtil.removePreAndLowerFirst(genQo.getTableName(), genQo.getPrefix())));// 表名去掉前缀后转驼峰
        contextConfig.setBizEnBigName(StrUtil.upperFirst(contextConfig.getBizEnName()));

        contextConfig.setProjectPath(StrUtil.isEmpty(genQo.getProjectPath())?System.getProperty("user.dir"):genQo.getProjectPath());//项目的绝对路径
        contextConfig.setEntityName(StrUtil.upperFirst(contextConfig.getBizEnName()));
        
    }
    
  //自定义配置
    protected void sqlConfig() {
        sqlConfig.setParentMenuName(null);//这里写已有菜单的名称,当做父节点
    }
 
}
